﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;

public class Collect : MonoBehaviour {

    private int exp;//经验值，用来升级
    private int level;//等级

    private ScoreManager m_scoreManager;
    private Attack m_attack;
    private Text m_expText;
    private Text m_levelText;
    private PlayerMove m_playerMove;
    private PlayerHealth m_playerHealth;

    private void Awake()
    {
        m_scoreManager = GameObject.Find("GameManager").GetComponent<ScoreManager>();
        m_attack = GetComponent<Attack>();
        m_expText = GameObject.Find("Exp").GetComponent<Text>();
        m_levelText = GameObject.Find("Level").GetComponent<Text>();
        m_playerMove = GetComponent<PlayerMove>();
        m_playerHealth = GetComponent<PlayerHealth>();
    }

    private void Start()
    {
        level = 0;
    }

    private void Update()
    {
        if (exp == 1 && level == 0)
        {
            LevelUP();
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Item")
        {
            m_scoreManager.AddScore(1);
            exp++;
            UpdateExpUI();
            Destroy(other.gameObject);
        }
    }

    /// <summary>
    /// 升级
    /// </summary>
    private void LevelUP()
    {
        level++;
        UpdateLevelUI();
        if (level == 1)
        {
            //加载player2模型
            GameObject go = Resources.Load("Player2") as GameObject;
            Instantiate(go, transform.position, transform.rotation, transform);
            transform.position += Vector3.up * 2;

            //删除player1模型
            GameObject child = GameObject.Find("Player1");
            Destroy(child.gameObject);

            //调整摄像头位置
            Transform m_eye = GameObject.Find("Eye").transform;
            m_eye.position += Vector3.up * 1;

            //调整charactercontroller
            CharacterController characterController = GetComponent<CharacterController>();
            characterController.center = Vector3.down * 0.2f;
            characterController.radius = 1.35f;
            characterController.height = 4.53f;

            //设置fire position
            Transform firePos = GameObject.FindGameObjectWithTag("FirePosition").transform;
            firePos.parent = transform;
            firePos.position = transform.TransformPoint(1.13f, 0.426f, 1.568f);
            firePos.rotation = transform.rotation;
            Transform firePos2 = Instantiate(firePos,transform) as Transform;
            firePos2.position = transform.TransformPoint(-0.99f, 0.35f, 1.61f);
            m_attack.ReloadFirePos();

            m_attack.ReloadAnimator();
            m_playerMove.ReloadAnimator();
            m_playerHealth.ReloadAnimator();



        }
    }

    /// <summary>
    /// 更新经验值UI
    /// </summary>
    private void UpdateExpUI()
    {
        m_expText.text = "Exp: " + exp;
    }

    /// <summary>
    /// 更新等级UI
    /// </summary>
    private void UpdateLevelUI()
    {
        m_levelText.text = "Level: " + level;
    }

}
